package com.zhao.controller.show;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Chart;
import com.zhao.pojo.Statistic;
import com.zhao.pojo.User;
import com.zhao.service.ChartService;
import com.zhao.service.StatisticService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@Controller
@RequestMapping("/show")
public class StatisticController {
    @Autowired
    StatisticService statisticService;
    @Autowired
    ChartService chartService;


    @ResponseBody
    @RequestMapping("/zan/{bauthor}/{id}/{type}")
    public String zan(@PathVariable(value = "bauthor")String bauthor,
                      @PathVariable(value = "id")Integer id,
                      @PathVariable(value = "type")Integer type,
                      @RequestParam(required = false)String groupname){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        //chart表 添加数据
        if (loginUser==null){
            return "-1";
        }
        Chart chart = chartService.getOne(new QueryWrapper<Chart>().eq("aid", id).eq("author", loginUser.getUserAccount()).eq("type",type));
        Statistic byId = statisticService.getOne(new QueryWrapper<Statistic>().eq("article_id",id));
        // null 新加 非空修改
        if (chart==null){
            chart=new Chart();
            chart.setAuthor(loginUser.getUserAccount());//谁
            chart.setType(type); // 点赞/收藏了
            chart.setBauthor(bauthor); //了谁
            chart.setAid(id); //的那篇文章

            //统计表添加数据
            if (type==0){
                byId.setZan(byId.getZan()+1);
            }else {
                chart.setGroupname(groupname);
                byId.setCollection(byId.getCollection()+1);
            }
            chartService.save(chart);
            statisticService.updateById(byId);
        }else {
           chartService.removeById(chart.getId());
            if (type==0){
                byId.setZan(byId.getZan()-1);
            }else {
                byId.setCollection(byId.getCollection()-1);
            }
            statisticService.updateById(byId);
        }
        return "1";
    }

    @RequestMapping("/collectGroup/{bauthor}/{id}")
    public String collectGroup(@PathVariable(value = "bauthor")String bauthor,@PathVariable(value = "id")Integer id,Model model){
        Chart chartNullId=new Chart();
        chartNullId.setBauthor(bauthor);
        chartNullId.setAid(id);
        System.out.println("======================================>"+id);
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<Chart> charts = chartService.list(new QueryWrapper<Chart>().eq("author", loginUser.getUserAccount()).eq("type",1).groupBy("groupname"));
        if (charts.size()==0){
            charts =new ArrayList<>();
            Chart chart = new Chart();
            chart.setGroupname("默认收藏夹");
            charts.add(chart);
            model.addAttribute("charts",charts);
        }else {
            model.addAttribute("charts",charts);
        }
        model.addAttribute("chart",chartNullId);
        return "show/collectGroup";
    }
    //@MyLog(value = "修改收藏夹名")
    @ResponseBody
    @RequestMapping("/updateGroupName/{oldName}/{newName}")
    public String updateGroupName(@PathVariable(required = false) String oldName,@PathVariable(required = false) String newName,@RequestParam(value = "maps[]",required = false)@RequestBody int[] maps){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<Chart> list;
        //判断 aid 是否为null 真 修改全部 否则修改部分
        if (maps==null||maps.length==0){
            //获取当前用户的 所有叫 oldName 收藏
            list = chartService.CollectByAll(loginUser.getUserAccount(),oldName);
        }else {
            list=new ArrayList<>();
            for (int aid:maps) {
                list.add(chartService.getById(aid));
            }
        }
        for (Chart chart:list) {
            chart.setGroupname(newName);
            chartService.updateById(chart);
        }
        return "1";
    }
}
